package com.sword.onlysword.service.games.impl;

import com.sword.onlysword.entity.Sudoku;
import com.sword.onlysword.games.SudokuBuilder;
import com.sword.onlysword.mapper1.games.SudokuMapper;
import com.sword.onlysword.service.games.SudokuService;
import com.sword.onlysword.web.vo.SudokuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * com.sword.onlysword.service.games.impl.WebGamesServiceImpl
 * Created by SwordLiu on 2018-02-03.
 */
@Service
public class SudokuServiceImpl implements SudokuService {

    @Autowired
    private SudokuMapper sudokuMapper;

    @Override
    public SudokuVo sudoku(Sudoku sudoku) {
        Optional<Sudoku> sudokuParam = Optional.ofNullable(sudoku);
        SudokuVo su = new SudokuVo();
        su.setSudoku(SudokuBuilder.random(sudoku));
        su.setDifficulty(sudokuParam.map(Sudoku::getDifficulty).orElse(1));
        su.setSudokus(sudokuMapper.list(su.getDifficulty(), 100));
        return su;
    }

    @Override
    public boolean sudokuScore(Sudoku sudoku) {
        int count = sudokuMapper.insert(sudoku);
        return count > 0;
    }

}
